package com.palnar.ilecker.ui.common;

import java.util.ArrayList;
import java.util.Arrays;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.palnar.ilecker.R;
import com.palnar.ilecker.adapter.MenuListAdapter;
import com.palnar.ilecker.baseactivity.BaseActivity;
import com.palnar.ilecker.constants.IServiceConstants;
import com.palnar.ilecker.manager.AllServiceManager;
import com.palnar.ilecker.manager.AllServiceManager.SoapWithArgsListener;
import com.palnar.ilecker.manager.CustomProgressDialog;
import com.palnar.ilecker.manager.NotifyManager;
import com.palnar.ilecker.manager.SelectorManager;
import com.palnar.ilecker.manager.ServiceManager;
import com.palnar.ilecker.model.MenuListModel;
import com.palnar.ilecker.model.ServiceManagerModel;
import com.palnar.ilecker.model.User;
import com.palnar.ilecker.ui.menu.Menu_Activity;
import com.palnar.ilecker.utils.Utils;

public class Login_Activity extends BaseActivity implements OnClickListener,
		IServiceConstants {

	private Context context;

	private Intent i;

	// private Button btnLanguage;
	private Button btnLogin;
	private Button btnResetPassword;

	private EditText edtUserName;
	private EditText edtPassword;
	private ServiceManager serviceManager;
	private AllServiceManager allServiceManager;

	private CustomProgressDialog progress;

	private User user;

	private MenuItem mSpinnerItem1 = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		context = this;
		setupAd();
		intializeUI();
		setListners();
		setSelectionManager();
	}

	private void intializeUI() {

		btnLogin = (Button) findViewById(R.id.btnLogin);
		// btnLanguage = (Button) findViewById(R.id.btnLanguage);
		btnResetPassword = (Button) findViewById(R.id.btnResetPassword);

		edtUserName = (EditText) findViewById(R.id.edtUserName);
		edtPassword = (EditText) findViewById(R.id.edtPassword);

		progress = CustomProgressDialog.show(context, false);

		// ActionBar bar = getActionBar();
		// bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		
		
		
	}

	private void setListners() {

		btnLogin.setOnClickListener(this);
		// btnLanguage.setOnClickListener(this);
		btnResetPassword.setOnClickListener(this);

	}

	private void setSelectionManager() {

		/*
		 * SelectorManager.setBackground(btnLanguage, SelectorManager
		 * .getButtonDrawableByScreenCathegory(context,
		 * R.drawable.englishlanguag, R.drawable.germanlanguage));
		 */
		SelectorManager.setBackground(btnLogin, SelectorManager
				.getButtonDrawableByScreenCathegory(context,
						R.drawable.global_top_button_normal,
						R.drawable.global_top_button_pressed));

		btnResetPassword.setTextColor(SelectorManager.getTextColor(
				getResources().getColor(android.R.color.black), getResources()
						.getColor(android.R.color.darker_gray)));

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater mi = getMenuInflater();
		mi.inflate(R.menu.configuration, menu);
		mSpinnerItem1 = menu.findItem(R.id.menu_spinner1);
		View view1 = mSpinnerItem1.getActionView();
		if (view1 instanceof Spinner) {
			String[] values = new String[] { "English", "Germany" };

			ArrayList<MenuListModel> menuListModels = new ArrayList<MenuListModel>();

			menuListModels.add(new MenuListModel("English",
					R.drawable.ic_english));
			menuListModels
					.add(new MenuListModel("German", R.drawable.ic_german));

			MenuListAdapter menuListAdapter = new MenuListAdapter(
					menuListModels, context, Color.WHITE);

			final Spinner spinner = (Spinner) view1;
			spinner.setAdapter(menuListAdapter);

			spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {

				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub

				}
			});

		}

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			i = new Intent(context, ConfigSetting_Activity.class);
			startActivity(i);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		if (btnLogin == v) {

			ServiceManagerModel getUser = new ServiceManagerModel(
					USER_SERVICE_WSDL, METHOD_GETUSER, SOAP_ACTION_FOR_GETUSER,
					new ArrayList(Arrays.asList("username", "password")),
					new ArrayList(Arrays.asList(edtUserName.getText()
							.toString(), edtPassword.getText().toString())));

			/*ServiceManagerModel getSEP = new ServiceManagerModel(SEP_WSDL,
					METHOD_GETSERVICEENDPOINT,
					SOAP_ACTION_FOR_GETSERVICEENDPOINT);

			ServiceManagerModel getSST = new ServiceManagerModel(SEP_WSDL,
					METHOD_GETSERVICESTATIONTYPE,
					SOAP_ACTION_FOR_GETSERVICESTATIONTYPE);

			ServiceManagerModel getMenuLanguage = new ServiceManagerModel(
					MENUSERVICEWSDL, METHOD_GETMENULANGUAGES,
					SOAP_ACTION_FOR_GETMENULANGUAGES);*/

			allServiceManager = new AllServiceManager(
					new ArrayList<ServiceManagerModel>(Arrays.asList(getUser)),
					progress);

			allServiceManager
					.setAllWithServiceListener(new SoapWithArgsListener() {

						@Override
						public void dataDownloadedSuccessfully(
								ArrayList<SoapObject> result,
								ArrayList<String> method) {

							Utils.longInfo(result.toString());
							System.out.println("SIZE= "
									+ result.get(0).getPropertyCount());

							if (result.size() > 0) {

								if ((result.get(0) != null)
										&& (method.get(0)
												.equals(METHOD_GETUSER)))

								{

									user = User.UserTypeBuilder(result.get(0));

									if (user.GetId() != 0) {

										NotifyManager
												.showLongToast(
														context,
														getString(R.string.login_success));
										i = new Intent(context,
												Menu_Activity.class);
										startActivity(i);

									} else {

										NotifyManager.showLongToast(context,
												getString(R.string.login_err));
									}

								}

							}

						}

						@Override
						public void dataDownloadedFailed(String error) {
							System.out.println("FAILED   " + error);

						}
					});

			allServiceManager.execute();

			// i=new Intent(context,Menu_Activity.class);
			// startActivity(i);

		} else if (btnResetPassword == v) {

		}

	}

}
